package com.cyh.nio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

/**
 *  异步写2
 * */
public class AsyncFileChannelDemo04 {


    public static void main(String[] args) throws Exception {
       //1 创建AsynchronousFileChannel
        Path path = Paths.get("D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\5.txt");
        AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);

        //2 创建Buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        //3 write 方法
        buffer.put("bbbb".getBytes());
        buffer.flip();


        fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(Integer result, ByteBuffer attachment) {

            }

            @Override
            public void failed(Throwable exc, ByteBuffer attachment) {

            }
        });

        buffer.clear();

    }
}
